package com.security.handler;

import com.security.exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常处理器
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-11-01 17:02
 **/
// 类下边的方法都是处理其他Controller抛出来的异常的
@ControllerAdvice
public class ControllerExceptionHandler {

    // 该处处理的异常是在interceptor的afterCompletion之前，在afterCompletion中是拿不到该异常的
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> handleCustomException(CustomException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", ex.getCode());
        result.put("message", ex.getMessage());
        return result;
    }

}
